﻿
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace GameLogic
{
    /// <summary>
    /// 资源路径信息配置表。
    /// 1、将ResourcePathConfig.json文件初始化为字典对象。
    /// </summary>
    [System.Serializable]
    [CreateAssetMenu(menuName = "自定义/资源路径配置文件")]
    public class ResourcePathConfig:ScriptableObject
    {
        /// <summary>
        /// 资源所属路径列表
        /// </summary>
        [SerializeField]
        private List<string> resPathList;
        /// <summary>
        /// 资源隶属AssetBundle的名称
        /// </summary>
        [SerializeField]
        private List<string> resAbNameList;
        /// <summary>
        /// 资源信息列表只用于生成Json序列化
        /// </summary>
        [SerializeField]
        private List<ResPathInfo> resInfoList;
        /// <summary>
        /// 资源路径信息表，这个表是ResourcePathConfig.json中的内容，它是在开发环境中生成。
        /// </summary>
        private Dictionary<string, ResPathInfo> resPathInfoMap;


        /// <summary>
        /// 初始化配置表数据
        /// </summary>
        public void Init(Dictionary<string, int> resPathMap, Dictionary<string, int> resAbNameMap, Dictionary<string, ResPathInfo> resInfoMap)
        {
            resPathList = new List<string>();
            resPathList.AddRange(resPathMap.Keys);

            resAbNameList = new List<string>();
            resAbNameList.AddRange(resAbNameMap.Keys);

            resInfoList = new List<ResPathInfo>();
            resInfoList.AddRange(resInfoMap.Values);
        }

        public void Init(List<string> resList,List<string> abNames,Dictionary<string,ResPathInfo> resInfos) {
            this.resPathList = resList;
            this.resAbNameList = abNames;
            this.resInfoList = new List<ResPathInfo>();
            resInfoList.AddRange(resInfos.Values);
        }

        /// <summary>
        /// 解析配置表
        /// </summary>
        public void AnalyticData()
        {
            if (resPathInfoMap != null) return;
            resPathInfoMap = new Dictionary<string, ResPathInfo>();
            for (int i = 0; i < resInfoList.Count; ++i)
            {
                var info = resInfoList[i];
                resPathInfoMap.Add(info.resName, info);
            }
        }

        /// <summary>
        /// 根据资源名称获取资源目录路径
        /// </summary>
        public string GetPath(string resName)
        {
            ResPathInfo info = null;
            if (resPathInfoMap.TryGetValue(resName,out info))
            {
                return resPathList[info.resPathIndex]+"/"+ resName + info.extension;
            }
            return string.Empty;
        }

        /// <summary>
        /// 根据资源名称获取资源所属AB名称
        /// </summary>
        public string GetABName(string resName)
        {
            ResPathInfo info = null;
            if (resPathInfoMap.TryGetValue(resName, out info))
            {
                return resAbNameList[resPathInfoMap[resName].resAbNameIndex]+AppConst.ABExtName;
            }
            return string.Empty;
        }
    }


    /// <summary>
    /// 资源路径属性
    /// </summary>
    [System.Serializable]
    public class ResPathInfo
    {
        /// <summary>
        /// 资源名称
        /// </summary>
        [SerializeField]
        public string resName;
        /// <summary>
        /// 资源名称（带后缀）
        /// </summary>
        [SerializeField]
        public string extension;
        /// <summary>
        /// 资源路径下标
        /// </summary>
        [SerializeField]
        public int resPathIndex;
        /// <summary>
        /// AB名字下标
        /// </summary>
        [SerializeField]
        public int resAbNameIndex;

        public ResPathInfo(string resName,string resNameExt,int resPathIndex, int resAbNameIndex)
        {
            this.resName = resName;
            this.extension = resNameExt;
            this.resPathIndex = resPathIndex;
            this.resAbNameIndex = resAbNameIndex;
        }
    }

}


